<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="../includes/tag.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Wcms内容管理系统</title>
    <script type="text/javascript" src="<%=basePath%>resources/lib/jquery/jquery.js"></script>
    <script type="text/javascript" src="<%=basePath%>resources/lib/webui/buss.ui.core.js"></script>
    <script type="text/javascript" src="<%=basePath%>resources/lib/webui/buss.ui.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/jqgrid/js/i18n/grid.locale-cn.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/jqgrid/js/jquery.jqGrid.min.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/public.js"></script>
	
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/styles/themes/buss-ui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/jqgrid/css/jquery-ui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/jqgrid/css/ui.jqgrid.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>resources/styles/public.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>resources/styles/icon.css">

	<script type="text/javascript">
		$(document).ready(function(){						
			setGridData();
			loadTree();
			resize();
			$(window).resize(resize);
			$("#search-btn").button({iconCls:'icon-search',handler:function(){
				var jsonParam = syscore.formToJson($("form[name='_searchForm']"));
				$("#tabledata").jqGrid("setGridParam",{page:1,postData:jsonParam}).trigger("reloadGrid");				
			}});
			$("#add-btn").button({iconCls:'icon-add',handler:function(){
				var node = $("#organTree").treeview("getSelectedNode");
				if(!node || node.id == '0'){
					$.alert('提示','请选择要增加用户所属的组织机构!',300,140,'info');
					return;
				}
				var url = '<%=basePath%>admin/user/addpage.do';
				var addUrl = '<%=basePath%>admin/user/add.do';
				var dlg = syscore.editDialog('新增用户信息',url,'organId=' + node.id,500,350,addUrl,function(rsData){
				
					if(rsData.message.code != '0'){
						 $.alert('提示','保存数据失败!原因:' + rsData.message.mess,300,140,'error');
						 return false;
					 }else{
	       			 	 $("#tabledata").jqGrid().trigger('reloadGrid');				        			 	
	       			 	 dlg.dialog("destroy");
					 }	
				});
			}});		
			$("#edit-btn").button({iconCls:'icon-edit',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows == 0){
					$.alert('提示','你没有选择要修改的数据!',300,140,'info');
					return;
				}
				if(selRows.length > 1){
					$.alert('提示','只能选择一条数据，请重新选择!',300,140,'info');
					return;
				}
				var url = '<%=basePath%>admin/user/editpage.do';
				var editUrl = '<%=basePath%>admin/user/edit.do';
				var dlg = syscore.editDialog('修改用户信息',url,'userId='+selRows[0],500,350,editUrl,function(rsData){
					if(rsData.message.code != '0'){
						 $.alert('提示','保存数据失败!原因:' + rsData.message.mess,300,140,'error');
						 return false;
					 }else{						 
        			 	 $("#tabledata").jqGrid().trigger('reloadGrid');			        			 	
	       			 	 dlg.dialog("destroy");
					 }	
				},function(){
					dlg.find("input[name='userId']").attr("readonly","readonly");
				});	
				
			}});
			$("#del-btn").button({iconCls:'icon-remove',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows.length == 0){
					$.alert('提示','你没有选择要删除的数据!',300,140,'info');
					return;
				}
				
				$.confirm('提示','确认要删除选中数据吗?',300,140,function(){
					var str = "";
					for(var i = 0;i < selRows.length;i++){
						if(i < selRows.length - 1){
							str += "ids=" + selRows[i] + "&";
						}else{
							str += "ids=" + selRows[i];
						}
					}
					$.post("<%=basePath%>admin/user/delete.do",str,function(rsData){
						if(rsData.message.code != '0'){
							$.alert('错误','删除系统菜单信息失败!原因:' + rsData.message.mess,300,140,'error');
							return;
						}
						$("#tabledata").jqGrid("setGridParam",{page:1}).trigger("reloadGrid");	
					},"json");
				});				
			}});
			$("#role_sett").button({iconCls:'icon-remove',handler:function(){
				//<table id="tabledata"></table>
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows.length == 0){
					$.alert('提示','你没有选择要设置角色的人员!',300,140,'info');
					return;
				}
				if(selRows.length > 1){
					$.alert('提示','只能选择一条数据，请重新选择!',300,140,'info');
					return;
				}
				var dlg = $("<div><div style='margin:5px;'><form action='<%=basePath%>admin/user/editRoleToUser/"+selRows[0]+".do' method='post'><table id='rolesdata'/></form></div></div>").dialog({
					modal:true,
					header:true,
					width:500,
					height:342,
					title:'角色信息',
					closable:true,
					iconCls:'icon',					
					buttons:[
						{text:'确  定',type:'button',iconCls:'icon-save',handler:function(){
							syscore.ajaxSubmit(dlg.find("form"),function(rsData){
								if(rsData.message.code != "0"){
									$.alert('错误','保存员工对应角色信息失败!原因:' + rsData.message.mess,300,140,'error');
					    			 return;
								}
								dlg.dialog("destroy");
							});
						}},
						{text:'关  闭',type:'button',iconCls:'icon-cancel',handler:function(){
					    	dlg.dialog("destroy");
					 }}]
				});
				setRolesData(dlg.find("#rolesdata"),selRows[0]);
			}});

			function setRolesData(obj,userId){
				var url = "<%=basePath%>admin/role/listByUserId/"+userId+".do";
				var colNames = ['选择','id','角色名称','角色简介'];
				var colModel = [
				                {name:'checked',sortable:false,editable:true,width:60,align:'center',formatter:function(cellValue,opts,rowObj){
				                	var h = "";
				                	if(cellValue == true || cellValue == 'true'){
				                		h = "<input type='checkbox' checked='checked' name='ids' value="+rowObj.id+">";
				                	}else{
				                		h = "<input type='checkbox' name='ids' value="+rowObj.id+">";
				                	}
				                	return h;
				                }},
								{name:'id',index:'id', width:60, hidden:true,sortable:false},
							    {name:'name',index:'name', width:100,align:'left',sortable:false},
								{name:'remark',index:'remark', width:300,align:'left',sortable:false}
							];
				$(obj).jqGrid({
					url:url,
					datatype:'json',
					mtype:'post',
					height:278,
					multiselect: false,
					autowidth:true,
					autoencode:true,
					viewrecords:true,
					forceFit:true,
					gridview:true,
					loadui:'block',
					rowNum:0,
					sortorder:'asc',
					rownumbers:true,
					colNames:colNames,
					colModel:colModel,
					jsonReader:{
						root:'result.data',
						page:'result.page.curPageNo',
						total:'result.page.totalPage',
						repeatitems:false
					},
					pager:""
				});
			}
			
			$("#organ_change").button({iconCls:'icon-remove',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows.length == 0){
					$.alert('提示','你没有选择要调动部门的人员!',300,140,'info');
					return;
				}
				var dlg = $("<div><ul></ul></div>").dialog({
					modal:true,
					header:true,
					width:400,
					height:450,
					title:'部门调动',
					closable:true,
					iconCls:'icon',
					buttons:[
					     {text:'确  定',type:'button',iconCls:'icon-save',handler:function(){
					    	 var saveUrl = "<%=basePath%>admin/user/organTransfer.do";
					    	 var userIds = "";
					    	 var selNodes = dlg.find("ul").treeview("getCheckedNodes");
					    	 if(selNodes.length == 0){
					    		 $.alert("提示","请选择要调入的部门机构!",300,140,"info");
					    		 return;
					    	 }
					    	 var organId = selNodes[0].id;
					    	 
					    	 for(var i = 0;i < selRows.length;i++){
					    		 if(i < selRows.length - 1){
					    		 	userIds += "userIds=" + selRows[i] + "&";
					    		 }else{
					    			 userIds += "userIds=" + selRows[i];
					    		 }
					    	 }
					    	 userIds += "&organId=" + organId;
					    	 $.post(saveUrl,userIds,function(rsData){
					    		 if(rsData.message.code != "0"){
					    			 $.alert('错误','部门调动失败!原因:' + rsData.message.mess,300,140,'error');
					    			 return;
					    		 }
					    		 $("#tabledata").jqGrid("setGridParam",{page:1}).trigger("reloadGrid");	
					    		 dlg.dialog("destroy");
					    	 },"json");
					     }},
					     {text:'关  闭',type:'button',iconCls:'icon-cancel',handler:function(){
						    	dlg.dialog("destroy");
						 }}
					]
				});
				var root = [{
					id:"0",
					name:'组织机构',			
					isParent:true,
					hasNode:true,
					open:true
				}];
				dlg.find("ul").treeview({
					treeNodeKey:'id',
					treeNodeParentKey:'parentId',
					hasChildKey:'hasNode',
					checkable:true,
					checkStyle:"radio",
					nameCol:'name',
					nodesCol:'nodes',
					expandSpeed:"",
					asyncParam:['parentId'],
					async:true,
					root:{isRoot:true,nodes:root},
					asyncUrl:"<%=basePath%>admin/organization/listByTree/0.do"
				});
			}});
			
			$("#edit_password").button({iconCls:'icon-remove',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
								
				if(selRows.length == 0){
					$.alert('提示','你没有选择要修改密码的人员!',300,140,'info');
					return;
				}
				if(selRows.length > 1){
					$.alert('提示','只能选择一位要修改密码的人员!',300,140,'info');
					return;
				}
				var url = "<%=basePath%>admin/user/editUserPassPage/" + selRows[0] + ".do";
				var dlg = $("<div>").dialog({
					modal:true,
					header:true,
					width:400,
					height:200,
					title:'部门调动',
					closable:true,
					iconCls:'icon',
					loadUrl:url,
					buttonGap:30,
					onLoad:function(){
						
					},
					buttons:[
					         {text:'确定',type:'button',iconCls:'icon-save',handler:function(){
					        	 var password = dlg.find("input[name='password']").val();
					        	 var passwordConfirm = dlg.find("input[name='password_confirm']").val();
					        	 if(password != passwordConfirm){
					        		 $.alert('提示','两次输入的密码不一致，请核实!',300,140,'info');
					        		 return;
					        	 }
					        	 //callFun(rsData,statusText,xhr,$form);
					        	 syscore.ajaxSubmit(dlg.find("form"),function(rsData){
					        		 if(rsData.message.code != 0){
					        			 $.alert('提示','修改密码失败，原因:' + rsData.message.mess,300,170,'error');
						        		 return;
					        		 }
					        		 dlg.dialog("destroy");
					        	 });
					         }},
							 {text:'取消',type:'button',iconCls:'icon-cancel',handler:function(){
					        	 dlg.dialog("destroy");
					         }}
					    ]
				});
				dlg.dialog("loadPage");
			}});
		});
		
		
		
		
		function setGridData(){
			var url = "<%=basePath%>admin/user/list/0.do";
			var colNames = ['id','用户编码','用户名称','联系电话', '移动电话','电子邮件','是否管理员','是否停用'];
			var colModel = [
							{name:'id',index:'id', hidden:true},
							{name:'userId',index:'userId', width:60, sortable:false,align:'left'},
						    {name:'userName',index:'userName', width:100,align:'left'},
							{name:'telephone',index:'telephone', width:60,align:'left',sortable:false}, 
							{name:'mobilePhone',index:'mobilePhone', width:60,align:'left',sortable:false},
							{name:'email',index:'email', width:60,align:'left',sortable:false},
							{name:'sysAdmin',index:'sysAdmin',width:60,align:'center',sortable:false,formatter:function(cellVal){
								if(cellVal) return "是";else return "否";
							}},
							{name:'disabled',index:'disabled',width:60,align:'center',sortable:false,formatter:function(cellVal){
								if(cellVal) return "停用";else return "启用";
							}}
						];
			$("#tabledata").jqGrid({
				url:url,
				mtype:'post',
				datatype:'json',
				height:250,
				multiselect: true,
				multiboxonly:true,
				autowidth:true,
				autoencode:true,
				viewrecords:true,
				forceFit:true,
				gridview:true,
				loadui:'block',
				rowNum:20,
				sortorder:'asc',
				rownumbers:true,
				colNames:colNames,
				colModel:colModel,
				jsonReader:{
					id:'id',
					root:'result.data',
					page:'result.page.curPageNo',
					total:'result.page.totalPage',
					repeatitems:false
				},
				prmNames:{
					page:'curPageNo',
					rows:'pageSize',
					sort:'orderField',
					order:'orderType'					
				},
				page:1,
				pagerpos:'left',
				pager:"tablepage"
			});
		}	
		
		function loadTree(){
			var root = [{
				id:"0",
				name:'组织机构',			
				isParent:true,
				hasNode:true,
				open:true
			}];
			$("#organTree").treeview({				
				treeNodeKey:'id',
				treeNodeParentKey:'parentId',
				hasChildKey:'hasNode',
				nameCol:'name',
				nodesCol:'nodes',
				expandSpeed:"",
				asyncParam:['parentId'],
				async:true,
				root:{isRoot:true,nodes:root},
				asyncUrl:function(treeNode){
					if(treeNode){
						return "<%=basePath%>admin/organization/listByTree/"+treeNode.id+".do";
					}else{
						return "<%=basePath%>admin/organization/listByTree/0.do";
					}
				},
				callback:{
					beforeAsyncSuccess:function(msg){
						if(msg.message.code == '-2'){
							$.alert('提示','你还没有登录或Session已经过期，请重新登录系统!',300,170,'info');
							return false;
						}
						if(msg.message.code == "-1"){
							$.alert("提示","获取信息失败，原因：" + msg.message.mess,300,170,'error');
							return false;
						}
						return true;
					},
					click:function(event,treeId,treeNode){
						var url = $("#tabledata").jqGrid("getGridParam","url");
						//alert(url);
						$("#tabledata").jqGrid("setGridParam",{"url":"<%=basePath%>admin/user/list/" + treeNode.id + ".do"}).trigger('reloadGrid');
						//alert(treeNode.id);
					}
				}
			});
		}
		
		

		
		function resize(){
			var h = $(window).height() - $(".nav_curloc").outerHeight() - $(".nav_search").outerHeight() - $(".nav_btn_bar").outerHeight() - 10;
			$(".context").height(h);
			$("#tabledata").jqGrid("setGridHeight",h - 50);
			$("#tabledata").jqGrid("setGridWidth",$(window).width() - $("#organTree").parent().width() - 12);
		}
		
	</script>
  </head>
  
  <body>
  	<div style="border-right:1px solid #68C4E9;position:absolute;width:180px;height:100%;left:0;overflow:auto;">
  		<ul id="organTree" class="tree"></ul>
  	</div>
  	<div style="height:100%;margin:0 0 0 181px;position:relative;">
  		<div class="nav_curloc">
	    	<label>您的当前位置：<bstag:navbar menuId="${menuid}"/></label>
	    </div>
	    <div class="nav_search" style="height:28px;">
	    	 <form action="empty.do" name="_searchForm"> 
		    	<table height="100%" cellpadding="0" cellspacing="0" >
		    		<tr>
		    			<td valign="middle">用户编码/名称：</td>
		    			<td valign="middle">
		    				<input type="text" size="25" maxlength="100" class="text" name="userName"/>
		    			</td>
		    			<td style="padding-left:5px;">
		    				<a id="search-btn" href="javascript:void(0);">查 询</a>
		    			</td>
		    		</tr>
		    	</table>
	    	 </form> 
	    </div>
	    <div class="nav_btn_bar">
	    	<table height="100%" cellpadding="0" cellspacing="0">
	    		<tr>
	    			<td valign="middle">
	    				<a id="add-btn" href="javascript:void(0);">新 增</a>
	    			</td>
	    			<td valign="middle"" style="padding-left:5px;">
	    				<a id="edit-btn" href="javascript:void(0);">修 改</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="del-btn" href="javascript:void(0);">删 除</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="edit_password" href="javascript:void(0);">设置密码</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="organ_change" href="javascript:void(0);">部门调动</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="role_sett" href="javascript:void(0);">角色设置</a>
	    			</td>
	    		</tr>
	    	</table>
	    </div>
	    <div class="context">
	    	<table id="tabledata"></table>
	    	<div id="tablepage"></div>
	    </div>
  	</div> 
    
  </body>
</html>
